//
//  UIBarButtonItem+CustomUI.m
//  TideKit
//
//  Created by Tider on 14-11-24.
//  Copyright (c) 2014年 TideKit. All rights reserved.
//

#import "UIBarButtonItem+CustomUI.h"



@implementation UIBarButtonItem(CustomUI)


+ (UIBarButtonItem*)leftBarItemWithImage:(UIImage*)image
                        highlightedImage:(UIImage*)highlightedImage
                                  target:(id)target
                                  action:(SEL)action
{
    return [UIBarButtonItem barItemWithImage:image
                            highlightedImage:highlightedImage
                                     xOffset:-5
                                      target:target
                                      action:action];
}


+ (UIBarButtonItem*)leftBarItemWithTitle:(NSString*)title
                         normalTextColor:(UIColor *)normalColor
                    highlightedTextColor:(UIColor *)highlightedColor
                                  target:(id)target
                                  action:(SEL)action
{
    return [UIBarButtonItem barItemWithTitle:title
                             normalTextColor:normalColor
                        highlightedTextColor:highlightedColor
                                     xOffset:-5
                                      target:target
                                      action:action];
}


+ (UIBarButtonItem*)rightBarItemWithImage:(UIImage*)image
                         highlightedImage:(UIImage*)highlightedImage
                                   target:(id)target
                                   action:(SEL)action
{
    return [UIBarButtonItem barItemWithImage:image
                            highlightedImage:highlightedImage
                                     xOffset:5
                                      target:target
                                      action:action];
}


+ (UIBarButtonItem*)rightBarItemWithTitle:(NSString*)title
                          normalTextColor:(UIColor *)normalColor
                     highlightedTextColor:(UIColor *)highlightedColor
                                   target:(id)target
                                   action:(SEL)action
{
    return [UIBarButtonItem barItemWithTitle:title
                             normalTextColor:normalColor
                        highlightedTextColor:highlightedColor
                                     xOffset:5
                                      target:target
                                      action:action];
}


+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image
                    highlightedImage:(UIImage*)highlightedImage
                             xOffset:(CGFloat)xoffset
                              target:(id)target
                              action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:highlightedImage forState:UIControlStateHighlighted];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, xoffset, 0, -xoffset)];
    }
    else
    {
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, -xoffset, 0, xoffset)];
    }
    
    UIBarButtonItem *customUIBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return customUIBarButtonItem;
}


+ (UIBarButtonItem*)barItemWithTitle:(NSString*)title
                     normalTextColor:(UIColor *)normalColor
                highlightedTextColor:(UIColor *)highlightedColor
                             xOffset:(CGFloat)xoffset
                              target:(id)target
                              action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:normalColor forState:UIControlStateNormal];
    [button setTitleColor:highlightedColor forState:UIControlStateHighlighted];
    [button.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [button setFrame:CGRectMake(0, 0, [button.titleLabel.text sizeWithFont:button.titleLabel.font].width + 3, 24)];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        [button setContentEdgeInsets:UIEdgeInsetsMake(0, xoffset, 0, -xoffset)];
    }
    else
    {
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, -xoffset, 0, xoffset)];
    }
    
    UIBarButtonItem *customUIBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return customUIBarButtonItem;
}


@end
